package com.learn.springboot.service.utils;

import java.util.ArrayList;
import java.util.List;

import com.learn.springboot.dao.mode.PageParameter;
import com.learn.springboot.dao.request.CommonMuliPrimaryKeyRequest;
import com.learn.springboot.dao.request.CommonPageResult;
import com.learn.springboot.dao.request.CommonPrimaryKeyRequest;
import com.learn.springboot.dao.request.CommonRequest;
import com.learn.springboot.dao.request.PageQueryRequest;
import com.learn.springboot.dao.request.base.Converter;
import com.learn.springboot.dao.request.base.PrimaryKeyRequest;
import com.learn.springboot.dao.response.CommonResponse;

/**
 * @author suzheng
 * @date 2024-05-17
 **/
public class ConverterUtil {
    public static <T, E> List<T> convertList(List<E> srcList, Converter<T, E> converter) {
        List<T> list = new ArrayList<>();
        for (E element : srcList) {
            if (converter.isValid(element)) {
                list.add((T) converter.convertFrom(element));
            }
        }

        return list;
    }

    /**
     * 转换成dao分页参数
     *
     * @param pageQueryRequest
     * @return
     */
    public static PageParameter convertToPageParameter(PageQueryRequest pageQueryRequest) {
        PageParameter pageParameter = new PageParameter();
        pageParameter.setCurrentPage(pageQueryRequest.getCurrentPage());
        pageParameter.setPageSize(pageQueryRequest.getPageSize());
        return pageParameter;
    }


    /**
     * 转换成分页结果
     *
     * @param data
     * @param pageParameter
     * @param <T>
     * @return
     */
    public static <T> CommonPageResult<T> convertToPageResult(List<T> data, PageParameter pageParameter) {
        CommonPageResult<T> pageResult = new CommonPageResult<T>();
        pageResult.setTotalCount(pageParameter.getTotalCount());
        pageResult.setTotalPage(pageParameter.getTotalPage());
        if (data != null) {
            pageResult.setData(data);
        }
        return pageResult;
    }


    /**
     * 转换成分页结果
     *
     * @param srcPageResult
     * @param converter
     * @param <T>
     * @param <E>
     * @return
     */
    public static <T, E> CommonPageResult<T> convertToPageResult(CommonPageResult<E> srcPageResult,
                                                                 Converter<T, E> converter) {
        CommonPageResult<T> pageResult = new CommonPageResult<T>();
        pageResult.setTotalCount(srcPageResult.getTotalCount());
        pageResult.setTotalPage(srcPageResult.getTotalPage());
        if (srcPageResult.getData() != null) {
            pageResult.setData(converter.convertList(srcPageResult.getData()));
        }
        return pageResult;
    }

    /**
     * 转换成分页结果
     *
     * @param srcPageResult
     * @param <T>
     * @param <E>
     * @return
     */
    public static <T, E> CommonPageResult<T> convertToPageResult(CommonPageResult<E> srcPageResult, List<T> datas) {
        CommonPageResult<T> pageResult = new CommonPageResult<T>();
        pageResult.setTotalCount(srcPageResult.getTotalCount());
        pageResult.setTotalPage(srcPageResult.getTotalPage());
        pageResult.setData(datas);
        return pageResult;
    }

    /**
     * 结果转换
     *
     * @param srcResponse
     * @param converter
     * @param <T>
     * @param <E>
     * @return
     */
    public static <T, E> CommonResponse<T> convertToResult(CommonResponse<E> srcResponse, Converter<T, E> converter) {
        CommonResponse<T> response = new CommonResponse<T>();
        response.setErrorMsg(srcResponse.getErrorMsg());
        response.setErrorCode(srcResponse.getErrorCode());
        response.setResult(converter.convertFrom(srcResponse.getResult()));
        response.setSuccess(srcResponse.isSuccess());
        return response;
    }

    public static <T> CommonPrimaryKeyRequest<T> convertToPrimaryKeyRequest(T id, CommonRequest request) {
        CommonPrimaryKeyRequest keyRequest = new CommonPrimaryKeyRequest();
        keyRequest.setId(id);
        keyRequest.setCurrentUserId(request.getCurrentUserId());
        return keyRequest;
    }

    public static <T> CommonPrimaryKeyRequest<T> convertToPrimaryKeyRequest(T id, String currentMemberId) {
        CommonPrimaryKeyRequest keyRequest = new CommonPrimaryKeyRequest();
        keyRequest.setId(id);
        keyRequest.setCurrentUserId(currentMemberId);
        return keyRequest;
    }

    public static <T> CommonPrimaryKeyRequest<T> convertToPrimaryKeyRequest(PrimaryKeyRequest<T> idRequest, String currentMemberId) {
        CommonPrimaryKeyRequest keyRequest = new CommonPrimaryKeyRequest();
        keyRequest.setId(idRequest.getId());
        keyRequest.setCurrentUserId(currentMemberId);
        return keyRequest;
    }

    public static <T> CommonPrimaryKeyRequest<T> convertToPrimaryKeyRequest(PrimaryKeyRequest<T> idRequest, CommonRequest request) {
        CommonPrimaryKeyRequest keyRequest = new CommonPrimaryKeyRequest();
        keyRequest.setId(idRequest.getId());
        keyRequest.setCurrentUserId(request.getCurrentUserId());
        return keyRequest;
    }



    public static <T> CommonPrimaryKeyRequest<T> convertToPrimaryKeyRequest(T id) {
        CommonPrimaryKeyRequest keyRequest = new CommonPrimaryKeyRequest();
        keyRequest.setId(id);
//		keyRequest.setCurrentUserId(currentMemberId);
        return keyRequest;
    }

    public static <T> CommonMuliPrimaryKeyRequest<T> convertToMuliPrimaryKeyRequest(List<T> ids,
                                                                                    CommonRequest request) {
        CommonMuliPrimaryKeyRequest keyRequest = new CommonMuliPrimaryKeyRequest();
        keyRequest.setIds(ids);
        keyRequest.setCurrentUserId(request.getCurrentUserId());
        return keyRequest;
    }

    public static <T> CommonMuliPrimaryKeyRequest<T> convertToMuliPrimaryKeyRequest(List<T> ids,
                                                                                    String currentMemberId) {
        CommonMuliPrimaryKeyRequest keyRequest = new CommonMuliPrimaryKeyRequest();
        keyRequest.setIds(ids);
        keyRequest.setCurrentUserId(currentMemberId);
        return keyRequest;
    }

    public static int calculateTotalPage(int pageSize, int totalCount) {
        int totalPage = totalCount / pageSize + ((totalCount % pageSize == 0) ? 0 : 1);
        return totalPage;
    }

    /**
     * 转换成分页结果
     *
     * @param data
     * @param totalCount
     * @param totalPage
     * @param <T>
     * @return
     */
    public static <T> CommonPageResult<T> convertToPageResult(List<T> data, long totalCount, long totalPage) {
        CommonPageResult<T> pageResult = new CommonPageResult<T>();
        pageResult.setTotalCount(totalCount);
        pageResult.setTotalPage(totalPage);
        if (data != null) {
            pageResult.setData(data);
        }
        return pageResult;
    }
}